import fly from '../request'
import { isTest, baseHost } from '../config'
import xml2json from 'xmlstring2json'

let host = baseHost + 'jintangApi/clientgateway/'
let octopusToken = ''
let octopusApiid = ''

if (isTest) {
	octopusToken = 'a1de59f8bc824349884786704e2143b3'
	octopusApiid = '33b6d95c-ab58-4353-9480-e7e31cb041ca'
} else {
	octopusToken = '789581981c3f4ca78041636413786243'
	octopusApiid = '6378161d-91c7-40ec-af82-aa83e74ba991'
}

let jinTangPost = (api, params, isResquest) => {
	let paramsString = {
		body: {
			head: {
				password: '123',
				userid: 'test'
			}
		}
	}
	if (params.org_code) {
		paramsString.body.head.org_code = params.org_code
	}
	paramsString.body.head.trans_no = api
	if (isResquest) {
		paramsString.body.resquest = params
	} else {
		paramsString.body.request = params
	}
	params = { XmlString: JSON.stringify(paramsString) }
	let queries = []
	let keys = Object.keys(params)
	keys.forEach(key => {
		queries.push(key + '=' + params[key])
	})
	let data = queries.join('&')
	return new Promise((resolve, reject) => {
		fly
			.post(host, data, {
				headers: {
					'octopus-token': octopusToken,
					'octopus-apiid': octopusApiid
				}
			})
			.then(response => {
				// resolve(JSON.parse(xml2json(response).string['#text']).body.response)
				resolve(response)
			})
			.catch(error => {
				reject(error)
			})
	})
}
let jinTangPostRes = (api, params) => {
	return jinTangPost(api, params, true)
}

//  -------------------------------------  接口

let getSwiperInfo = params => {
	return jinTangPost('XW0004', params)
}

let getNewsInfo = params => {
	return jinTangPost('ZX0002', params)
}

let getUserInfo = params => {
	return jinTangPost('SG0018', params)
}

let getCheckList = params => {
	return jinTangPost('ZX0001', params)
}

let sendCode = params => {
	return jinTangPost('SG0035', params)
}

let verifyCode = params => {
	return jinTangPost('SG0036', params)
}

let login = params => {
	return jinTangPost('SG0012', params)
}

let updatePaswd = params => {
	return jinTangPost('SG0013', params)
}

let getInspectList = params => {
	return jinTangPost('C03.02.09.00', params)
}

let getTestList = params => {
	return jinTangPost('C03.02.08.00', params)
}

let bindCard = params => {
	return jinTangPost('YKT1011', params)
}

let doctorRecord = params => {
	return jinTangPost('TYZF0008', params)
}

let doctorRecordList = params => {
	return jinTangPost('TYZF0003', params)
}

let doctorRecordDetail = params => {
	return jinTangPost('TYZF0004', params)
}

let hospitalRecord = params => {
	return jinTangPost('TYZF0016', params)
}

let hospitalRecordDetail = params => {
	return jinTangPost('TYZF0017', params)
}

let invoice = params => {
	return jinTangPost('DZFP0001', params)
}

let myOrder = params => {
	return jinTangPost('SG0016', params)
}

let cancelOrder = params => {
	return jinTangPost('SG0017', params)
}

let checkNumber = params => {
	return jinTangPost('YKT0009', params)
}

let hospitalInfo = params => {
	return jinTangPost('SG0002', params)
}

let departmentList = params => {
	return jinTangPost('SG0003', params)
}

let doctorList = params => {
	return jinTangPost('SG0005', params)
}

let doctorInfo = params => {
	return jinTangPost('SG0006', params)
}

let selectTime = params => {
	return jinTangPost('SG0021', params)
}

export default {
	getUserInfo, // 金唐接口获得用户信息
	getSwiperInfo, // 金唐轮播图
	getNewsInfo, // 获得新闻信息
	getCheckList, // 获取资讯顶部选择栏
	sendCode, // 发送验证码
	verifyCode, //验证验证码
	login, // 登录
	updatePaswd, //修改密码
	getInspectList, //检验报告
	getTestList, //检查报告
	bindCard, //绑定社保卡
	doctorRecord, //就诊记录
	doctorRecordList, //就诊记录列表
	doctorRecordDetail, //就诊记录详情
	hospitalRecord, //住院记录
	hospitalRecordDetail, //住院记录详情
	invoice, //电子发票
	myOrder, //我的预约
	cancelOrder, //退号
	checkNumber, //查询当前已叫到的号
	hospitalInfo, //医院简介
	departmentList, //获取科室列表
	doctorList, //获取挂号医生列表
	doctorInfo, //获取医生详细信息
	selectTime //获取预约时间段
}
